// import { routerRedux } from 'dva/router';
import router from 'umi/router';
import { login } from '@/services/login';
import settings from '@/settings';

export default {
  namespace: 'login',
  state: {},
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *login({ payload }, { put, call }) {
      const { code, message } = yield call(login, payload);
      // yield put(routerRedux.replace('/'));
      if (code === settings.code) {
        yield put({
          type: 'updateState',
          payload: {
            error: null,
          },
        });
        router.push({
          pathname: settings.home,
        });
      } else {
        yield put({
          type: 'updateState',
          payload: {
            error: {
              message,
            },
          },
        });
      }
    },
  },
};
